﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;

namespace Bootstrap.Security
{
    /// <summary>
    /// BootstrapAdmin 管理系统接口服务操作类
    /// </summary>
    public static class BootstrapAdminInterfaceServiceCollectionExtensions
    {
        /// <summary>
        /// 添加 BootstrapAdmin 接口服务
        /// </summary>
        /// <param name="services">服务容器集合</param>
        /// <param name="configure">HttpClient 配置回调方法</param>
        /// <returns>IServiceCollection 服务容器集合</returns>
        public static IServiceCollection AddBootstrapAdminInterfaceService(this IServiceCollection services, Action<HttpClient> configure = null)
        {
            services.AddHttpClient<BootstrapAdminHttpClient>((provider, client) =>
            {
                var authHost = provider.GetRequiredService<IConfiguration>()["BootstrapAdminAuthenticationOptions:AuthHost"] ?? "";
                client.BaseAddress = new Uri($"{authHost}/api/Interface/");
                configure?.Invoke(client);
            });
            services.AddBootstrapAdminInterface();
            return services;
        }
    }
}
